package november.stack;

import java.util.Stack;

/**
 * @author huangxin
 * @date 2019/11/15-15:38
 */
public class Calculator {
    public static void main(String[] args) {
        String expression = "1+1*100";

        Stack<Integer> numStack = new Stack<>();
        Stack<Character> operatorStack = new Stack<>();
        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        char ch = ' ';

        //用于拼接多位数字
        String keepNum = "";

        while (true) {
            ch = expression.substring(index, index + 1).charAt(0);
            if (isOpera(ch)) {
                //如果是运算符
                if (!operatorStack.isEmpty()) {
                    //查看栈顶
                    char peek = operatorStack.peek();
                    //如果当前操作符<=栈顶操作符 , 从符号栈顶pop一个符号, 数字栈pop两个数进行运算
                    if (priority(ch) <= priority(peek)) {
                        num2 = numStack.pop();//先pop出来的是num2
                        num1 = numStack.pop();
                        oper = operatorStack.pop();
                        res = cal(num1, num2, oper);//计算
                        numStack.push(res);//把结果push回去
                    }
                }

                //无论是否进入if, ch都会入栈
                operatorStack.push(ch);
            } else {
                //如果是数字
                keepNum += ch;

                //判断是否为最后一位
                if (index + 1 >= expression.length()) {
                    //如果是,直接入栈
                    numStack.push(Integer.parseInt(keepNum));
                } else {
                    //判断下一位是不是符号
                    if (isOpera(expression.substring(index + 1, index + 2).charAt(0))) {
                        //如果是符号,把数字push进栈
                        numStack.push(Integer.parseInt(keepNum));
                        keepNum = "";
                    }
                }
            }

            index++;
            if (index >= expression.length()) {
                break;
            }
        }

        while (true) {
            if (operatorStack.isEmpty()) {
                //如果符号栈为空
                break;
            }

            num2 = numStack.pop();//先pop出来的是num2
            num1 = numStack.pop();
            oper = operatorStack.pop();
            res = cal(num1, num2, oper);//计算
            numStack.push(res);

        }
        System.out.println("结果为:" + numStack.pop());


    }


    public static int priority(int oper) {
        if (oper == '*' || oper == '/') {
            return 1;
        } else if (oper == '+' || oper == '-') {
            return 0;
        } else {
            return -1;
        }
    }


    //计算方法
    public static int cal(int num1, int num2, int oper) {
        int result = 0;
        switch (oper) {
            case '+':
                result = num1 + num2;
                break;
            case '-':
                result = num1 - num2;
                break;
            case '*':
                result = num1 * num2;
                break;
            case '/':
                result = num1 / num2;
                break;
            default:
                break;
        }
        return result;
    }


    public static boolean isOpera(char c) {
        return "+-*/".indexOf(c) > -1;
    }
}
